package com.meeting.project.business.meeting.controller;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.meeting.framework.aspectj.lang.annotation.Log;
import com.meeting.framework.aspectj.lang.enums.BusinessType;
import com.meeting.project.business.meeting.domain.Meeting;
import com.meeting.project.business.meeting.domain.MeetingVO;
import com.meeting.project.business.meeting.service.IMeetingService;
import com.meeting.framework.web.controller.BaseController;
import com.meeting.framework.web.domain.AjaxResult;
import com.meeting.common.utils.poi.ExcelUtil;
import com.meeting.framework.web.page.TableDataInfo;
import com.meeting.project.business.record.domain.SignInRecord;
import com.meeting.project.business.record.service.ISignInRecordService;

/**
 * 会议信息Controller
 *
 * @author llmaoa
 * @date 2025-08-05
 */
@Controller
@RequestMapping("/business/meeting")
public class MeetingController extends BaseController {
    private String prefix = "business/meeting";

    @Autowired
    private IMeetingService meetingService;

    @Autowired
    private ISignInRecordService signInRecordService;

    @RequiresPermissions("business:meeting:view")
    @GetMapping()
    public String meeting() {
        return prefix + "/meeting";
    }

    /**
     * 查询会议信息列表
     */
    @RequiresPermissions("business:meeting:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Meeting meeting) {
        startPage();
        List<MeetingVO> list = meetingService.selectMeetingVOList(meeting);
        return getDataTable(list);
    }

    /**
     * 导出会议信息列表
     */
    @RequiresPermissions("business:meeting:export")
    @Log(title = "会议信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Meeting meeting) {
        List<MeetingVO> list = meetingService.selectMeetingVOList(meeting);
        if (CollectionUtils.isNotEmpty(list)) {
            List<SignInRecord> signInRecordList = signInRecordService.selectSignInRecordListByMeetingIds(list.stream().map(Meeting::getId).collect(Collectors.toList()));
            if (CollectionUtils.isNotEmpty(signInRecordList)) {
                Map<Long, List<SignInRecord>> signInRecordMap = signInRecordList.stream().collect(Collectors.groupingBy(SignInRecord::getMeetingId));
                list.forEach(item -> item.setSignInRecordList(signInRecordMap.get(item.getId())));
            }
        }
        ExcelUtil<MeetingVO> util = new ExcelUtil<>(MeetingVO.class);
        return util.exportExcel(list, "会议信息数据");
    }

    /**
     * 新增会议信息
     */
    @RequiresPermissions("business:meeting:add")
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存会议信息
     */
    @RequiresPermissions("business:meeting:add")
    @Log(title = "会议信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Meeting meeting) {
        return toAjax(meetingService.insertMeeting(meeting));
    }

    /**
     * 修改会议信息
     */
    @RequiresPermissions("business:meeting:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        Meeting meeting = meetingService.selectMeetingById(id);
        mmap.put("meeting", meeting);
        return prefix + "/edit";
    }

    /**
     * 修改保存会议信息
     */
    @RequiresPermissions("business:meeting:edit")
    @Log(title = "会议信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Meeting meeting) {
        return toAjax(meetingService.updateMeeting(meeting));
    }

    /**
     * 删除会议信息
     */
    @RequiresPermissions("business:meeting:remove")
    @Log(title = "会议信息", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(meetingService.deleteMeetingByIds(ids));
    }

    @RequiresPermissions("business:meeting:list")
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") Long id, ModelMap mmap) {
//        mmap.put("meeting", meetingService.selectMeetingById(id));
        mmap.put("meetingId", id);
        return "business/record/record";
    }
}
